<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>HOME</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">

		<!--标准mui.css-->
		<link rel="stylesheet" href="../css/mui.min.css">
		<!--App自定义的css-->
		<link rel="stylesheet" type="text/css" href="../css/app.css" />
		<script src="../script/mui.min.js" type="text/javascript" charset="utf-8"></script>
		<style type="text/css">
			.item {} .count {
				font-size: 14px;
			}
			.hastest {
				font-size: 16px;
			}
			.parentNode {
				background-color: #f8f8fa;
				border-left: 4px solid #30a4ed;
			}
			.mui-table-view-cell.mui-active {
				/*border-left: 4px solid #51d1c2;*/
			}
			.mui-table-view {
				background-color: #f8f8fa;
				color: #fff;
			}
			.mui-table-view-chevron .mui-table-view-cell {
				/*margin-left: -4px;*/
				
				height: 80px;
				background-color: #f8f8fa;
				color: #51d1c2;
				border-left: 4px solid #51d1c2;
			}
			.mui-table-view-chevron .mui-table-view-cell p {
				color: #51d1c2;
			}
			.mui-table-view-cell:after {
				background-color: #e2e2e4;
				right: 15px;
				/*background-color: #c8c7cc;*/
				/*right: 15px;*/
			}
			.mui-table-view-cell.mui-collapse .mui-table-view .mui-table-view-cell:after {
				background-color: #e2e2e4;
				left: 11px;
				right: 66px;
			}
			.mui-table-view-chevron .mui-table-view-cell:first-child:before {
				position: absolute;
				top: 0;
				right: 66px;
				left: 15px;
				height: 1px;
				content: '';
				-webkit-transform: scaleY(.5);
				transform: scaleY(.5);
				background-color: #e2e2e4;
			}
		</style>

	</head>

	<body>
		<!--<div class="mui-content">-->
		<ul class="mui-table-view" id="list">

		</ul>
		<!--</div>-->
	</body>
	<script src="../script/lokijs.min.js" type="text/javascript" charset="utf-8"></script>
	<script src="../script/db.js" type="text/javascript" charset="utf-8"></script>
	<script src="../script/template.js" type="text/javascript" charset="utf-8"></script>
	<script src="../script/template/helper.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/html" id="listTem">
		{{each}}
		<li class="mui-table-view-cell" onclick="openPractise({{$value.id}},{{$value.courseid}},{{$value.count}},'{{$value.title}}')">
			<a class="parentNode" style="line-height: 24px;" tapmode>
				<p class="mui-ellipsis-2 mui-pull-left" style="width: 90%;font-size: 16px;">{{$value.title}}(<span class="count"><span class="hastest" class="hastest">{{getDoingCount($value.id)}}</span>&nbsp;/&nbsp;<span class="total">{{$value.count}}</span></span>)</p>
				<span class="mui-pull-right" style="padding: 5px 5px;"><img src="../image/edit2.png" style="height: 30px;"/></span>
			</a>
		</li>
		{{/each}}
	</script>
	<script type="text/javascript">
		MYPAGE = {};

		function openPractise(id,courseid,count,title) {

			var target = 'course-'+courseid+'/lesson-'+id;
			var data = {
				target:target,
				id:id,
				courseid:courseid
			}
			if(count>0){
				api.openWin({
					name: 'practise',
					url: 'practise.html',
					pageParam: data
				});
			}else{
				api.toast({
					msg:"该章节下暂无题目哦!"
				});
			}
			
			var db = new loki('data');
			db.loadDatabase({}, function() {
				var c = db.getCollection('history');
				if (c == null) {
					c = db.addCollection('history');
				}
				
//				alert(JSON.stringify(c.data));
				var q = {
					'id': id
				};
				var time = Date.parse(new Date())/1000;
				var $line = c.findOne(q);
				if ($line == null) {
					var data = {
						'id': id,
						'courseid': courseid,
						'title': title,
						'time': time
					};
					c.insert(data);
				} else {
					$line.time = time;
					c.update($line)
				}
				db.saveDatabase();
			});	
		}

		function renewTotal() {
			var func = function(db) {
				var cid = localStorage.getItem('cid');
				var sql = 'SELECT * FROM lesson where courseid = ' + cid + ' order by seq asc';
				//				var sql2 = 'SELECT COUNT(*) as total FROM question where `target` = \'' + target + '\' AND `myans` IS NOT NULL';
				db.selectSql({
					name: 'data',
					sql: sql
				}, function(ret, err) {
					if (ret.status) {
						var html = template('listTem', ret.data);
						document.getElementById("list").innerHTML = html;
						//						alert(JSON.stringify(ret.data));
						////						var total = ret.data[0];
						////						document.getElementById("total").innerHTML = total.total;
					} else {
						api.toast({
							msg: err.msg
						});
					}
				});
			}
			APP.openQDb(func);
		}
		apiready = function() {
			MYPAGE.data = api.pageParam;
			renewTotal();
			api.addEventListener({
				name: 'courseSelected'
			}, function(ret, err) {
				renewTotal();
			});
			
		}
	</script>

</html>